﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;

namespace Joyintech.Admin.inpiao365.general.system.users
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //绑定部门（如果是修改部门则需排除其自身及其下级部门）
                DataBindsystemUser();

                //绑定系统角色
                DataBindRole();

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("user_id") != "")
                {
                    DataBindForm();
                }
            }
        }

        #region 绑定部门（如果是修改部门则需排除其自身及其下级部门）

        /// <summary>
        /// 绑定所有部门
        /// </summary>
        private void DataBindsystemUser()
        {
            this.ddlDeptPID.Items.Clear();

            List<systemDepartment> list = new List<systemDepartment>();

            list = new systemDepartment_BLL().GetModelList("deptPID=0");

            foreach (systemDepartment model in list)
            {
                this.ddlDeptPID.Items.Add(new ListItem(model.DeptName, model.DeptID.ToString()));

                //递归绑定下级部门
                DataBindsystemUserChild(model.DeptID, "--");
            }
        }

        /// <summary>
        /// 递归绑定下级部门
        /// </summary>
        /// <param name="menuPID"></param>
        private void DataBindsystemUserChild(int deptPID, string frontStr)
        {
            List<systemDepartment> list = new List<systemDepartment>();

            list = new systemDepartment_BLL().GetModelList("deptPID=0" + deptPID);

            foreach (systemDepartment model in list)
            {
                this.ddlDeptPID.Items.Add(new ListItem(frontStr + model.DeptName, model.DeptID.ToString()));

                //递归绑定下级部门
                DataBindsystemUserChild(model.DeptID, "--" + frontStr);
            }
        }

        #endregion

        #region 绑定系统角色

        /// <summary>
        /// 绑定系统角色
        /// </summary>
        private void DataBindRole()
        {
            DataSet ds = new DataSet();

            ds = new systemRole_BLL().GetAllList();

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.cblUserRoles.DataSource = ds.Tables[0];
                this.cblUserRoles.DataTextField = "RoleName";
                this.cblUserRoles.DataValueField = "RoleId";
                this.cblUserRoles.DataBind();
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new systemUser_BLL().GetList("UserId=" + DNTRequest.GetQueryString("user_id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);

                this.UserPassword.Text = Joyintech.Common.DEncrypt.DEncrypt.Decrypt(this.UserPassword.Text);
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表systemUser
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            systemUser model = new systemUser();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //密码格式化
            model.UserPassword = Joyintech.Common.DEncrypt.DEncrypt.Encrypt(model.UserPassword);

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("user_id") != "")
            {
                model.UserId = Convert.ToInt32(DNTRequest.GetQueryString("user_id").ToString());

                dataResult = new systemUser_BLL().Update(model);
            }
            else
            {
                dataResult = new systemUser_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion
    }
}